package com.example.hsbc.util;

/**
 * @ClassName:
 * @Author:
 * @Date:
 * @Description:
 */
public class Demo1 {
    private String cutLetter(StringBuilder letter) {
        int l = 0;
        while(l >= 0) {
            if(letter.length()<3 || l + 2 >= letter.length()) {
                break;
            }
            if(letter.charAt(l) == letter.charAt(l + 1) &&
                    letter.charAt(l+2) == letter.charAt(l + 1)) {
                letter.deleteCharAt(l);
                letter.deleteCharAt(l);
                letter.deleteCharAt(l);
                if(l>=2){
                    letter.insert(l, letter.charAt(l-1));
                    l -=2;
                } else if(l==1) {
                    letter.insert(l, letter.charAt(l-1));
                    l = 0;
                } else {//l==0
                    l = 0;
                }
            }else {
                l++;
            }
        }
        return letter.toString();
    }

    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("aabcccbbad");
        System.out.println(new Demo1().cutLetter(sb));
        StringBuilder sb1 = new StringBuilder("abbcccbaad");
        System.out.println(new Demo1().cutLetter(sb1));
        StringBuilder sb2 = new StringBuilder("bbba");
        System.out.println(new Demo1().cutLetter(sb2));
        StringBuilder sb3 = new StringBuilder("bba");
        System.out.println(new Demo1().cutLetter(sb3));
        StringBuilder sb4 = new StringBuilder("ba");
        System.out.println(new Demo1().cutLetter(sb4));
        StringBuilder sb5 = new StringBuilder("a");
        System.out.println(new Demo1().cutLetter(sb5));
        StringBuilder sb6 = new StringBuilder("abacccbaad");
        System.out.println(new Demo1().cutLetter(sb6));
    }
}